package com.haitaoit.chengquan.module.center.activity.info;

import android.content.Intent;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.QBaseActivity;
import com.haitaoit.chengquan.business.data.GetAddressListHttp;
import com.haitaoit.chengquan.business.data.SetDefaultAddressHttp;
import com.haitaoit.chengquan.business.http.bean.ParamObj;
import com.haitaoit.chengquan.module.center.adapter.MyAddressAdapter;
import com.haitaoit.chengquan.module.center.model.MyAddressModel;
import com.haitaoit.chengquan.module.center.model.MyAddressModel2;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.refreshview.XListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/5.
 */

public class QMyAddressActivity extends QBaseActivity implements View.OnClickListener {

    private ImageView back;
    private TextView  title;

    private TextView     address_null;
    private TextView     address_name;
    private TextView     address_phone;
    private TextView     address_moren_add;
    private LinearLayout center_top_rela;

    private XListView        listView;
    private LinearLayout     address_add_ll;
    private MyAddressAdapter myAddressAdapter;
    private MyAddressModel   myAddressModel;


    @Override
    public int getContentView() {
        return R.layout.activity_my_address;
    }

    @Override
    public void initView() {
        back = (ImageView) findViewById(R.id.back);
        title = (TextView) findViewById(R.id.title);
        address_name = (TextView) findViewById(R.id.address_name);
        address_phone = (TextView) findViewById(R.id.address_phone);
        address_moren_add = (TextView) findViewById(R.id.address_moren_add);
        center_top_rela = (LinearLayout) findViewById(R.id.center_top_rela);
        listView = (XListView) findViewById(R.id.xListView);
        address_add_ll = (LinearLayout) findViewById(R.id.address_add_ll);
        address_null = (TextView) findViewById(R.id.address_null);
        myAddressAdapter = new MyAddressAdapter(this);
        listView.setAdapter(myAddressAdapter);
        listView.aotuRefresh();
        listView.setPullLoadEnable(false);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {
        back.setOnClickListener(this);
        address_add_ll.setOnClickListener(this);
        center_top_rela.setOnClickListener(this);

        //条目点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position > 0) {
                    int pos = position - 1;
                    Intent intent = new Intent(QMyAddressActivity.this, MyAddressUpdateActivity.class);
                    intent.putExtra("address_id", myAddressAdapter.getDataList().get(pos).getAddress_id());
                    intent.putExtra("isDefault", false);
                    startActivity(intent);
                }
            }
        });

        //设置为默认的回调接口
        myAddressAdapter.onMethodBackListener(new MyAddressAdapter.AddressBack() {
            @Override
            public void methodBack(MyAddressModel2 myAddressModel2) {
                //                AppLog.D("myAddressModel2:"+myAddressModel2);
                //                AppLog.D("myAddressModel2.getName():"+myAddressModel2.getName());
                //                QMyAddressActivity.this.def_myAddressModel2=myAddressModel2;
                List<ParamObj> paramObjs = new ArrayList<ParamObj>();
                paramObjs.add(new ParamObj("user_id", PreferenceUtils.getPrefString("user_id", "")));
                paramObjs.add(new ParamObj("address_id", myAddressModel2.getAddress_id()));
                new SetDefaultAddressHttp(QMyAddressActivity.this, handler, GetHaveParam(paramObjs), 3, true);

            }
        });

        //下拉刷新和上拉加载更多。。。。。。
        listView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                curpage = 1;
                getHttpData(true);
            }

            @Override
            public void onLoadMore() {
                getHttpData(false);
            }
        });
    }


    @Override
    protected void updateUI(Message msg) {
        switch (msg.what) {
            case 1:
                myAddressModel = (MyAddressModel) msg.obj;
                if (myAddressModel.getDefault_address() == null) {
                    address_null.setVisibility(View.VISIBLE);
                    address_add_ll.setVisibility(View.GONE);
                    center_top_rela.setVisibility(View.INVISIBLE);
                } else {
                    address_null.setVisibility(View.GONE);
                    address_add_ll.setVisibility(View.VISIBLE);
                    setDefaultAddress(myAddressModel.getDefault_address());
                    center_top_rela.setVisibility(View.VISIBLE);
                }
                setData(myAddressModel.getList_address(), true);
                listView.aotuRefreshComplete();
                listView.setVisibility(View.VISIBLE);
                break;

            case 2:
                MyAddressModel myAddressModel1 = (MyAddressModel) msg.obj;
                setData(myAddressModel1.getList_address(), false);
                break;
            case 3:
                closeProgressDialog();
                curpage = 1;
                getHttpData(true);

                break;
        }
    }

    /*设置默认地址*/
    private void setDefaultAddress(MyAddressModel2 def_myAddressModel) {

        address_name.setText("收货人：" + def_myAddressModel.getName());
        address_phone.setText(def_myAddressModel.getPhone());
        SpannableString msp = new SpannableString("【默认地址】收货地址：" + def_myAddressModel.getAddress());
        msp.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.app_color)), 0,
                6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        address_moren_add.setText(msp);
    }

    private void setData(List<MyAddressModel2> homeItemObjs, boolean isFirst) {
        if (isFirst) {
            myAddressAdapter.getDataList().clear();
        }
        if (homeItemObjs != null && homeItemObjs.size() > 0) {

            myAddressAdapter.getDataList().addAll(homeItemObjs);
            if (homeItemObjs.size() < 10) {
                listView.setPullLoadEnable(false);
            } else {
                listView.setPullLoadEnable(true);
            }
        } else {
            ToastUtils.showToast( isFirst ? getString(R.string.app_pull_not_data) : getString(R.string.app_pull_already_last_page));
        }
        myAddressAdapter.notifyDataSetChanged();
        listView.aotuRefreshComplete();
        curpage++;
    }

    /*当前页*/
    private int curpage = 1;

    private void getHttpData(boolean isFirst) {
        List<ParamObj> paramObjs = new ArrayList<ParamObj>();
        paramObjs.add(new ParamObj("user_id", PreferenceUtils.getPrefString("user_id", "")));
        paramObjs.add(new ParamObj("page", String.valueOf(curpage)));
        paramObjs.add(new ParamObj("pagesize", Constant.PageSize));
        new GetAddressListHttp(this, handler, GetHaveParam(paramObjs), isFirst ? 1 : 2, false);

    }

    @Override
    public void failureOperation(Object obj) {
        super.failureOperation(obj);
        listView.aotuRefreshComplete();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.address_add_ll:
                startActivityForResult(new Intent(QMyAddressActivity.this, MyAddressAddActivity.class), 1);
                break;
            case R.id.center_top_rela:
                Intent intent = new Intent(QMyAddressActivity.this, MyAddressUpdateActivity.class);
                intent.putExtra("address_id", myAddressModel.getDefault_address().getAddress_id());
                intent.putExtra("isDefault", true);
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
            curpage = 1;
            listView.aotuRefresh();
        }
    }
}
